package com.hanbang.mrcm.merchant;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.hanbang.mrcm.R;
import com.hanbang.mrcm.base.BaseActivity;
import com.hanbang.mrcm.bean.HongBaoOpenData;
import com.hanbang.mrcm.utils.HttpInterfaces;
import com.hanbang.mrcm.utils.JsonHelper;
import com.hanbang.mrcm.utils.MyRequestCallBack;
import com.hanbang.mrcm.utils.SystemTools;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class HongBaoOpenActivity extends BaseActivity {

	private HongBaoOpenData data = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.hongbaoopen);
		bitmapUtils.configDefaultLoadFailedImage(R.drawable.default_portrait2);
		bitmapUtils.configDefaultLoadingImage(R.drawable.default_portrait2);
		initView();
		setOnClickListener();
		getServiceData();
	}

	/*
	 * 设置点击事件
	 */
	private void setOnClickListener() {
		findViewById(R.id.chakanpaiming).setOnClickListener(onClickListener);
	}

	private OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {

			case R.id.chakanpaiming:
				Intent intent = new Intent();
				intent.setClass(HongBaoOpenActivity.this,
						SharedRankActivity.class);
				intent.putExtra("type", getIntent().getIntExtra("type", 0));
				intent.putExtra("activity_id",
						getIntent().getIntExtra("activity_id", 0));
				startActivity(intent);
				break;

			default:
				break;
			}
		}
	};

	/*
	 * 初始化view
	 */
	private void initView() {
		// 设置顶部的标题
		setBackArrows(findViewById(R.id.top_bar_left_image));

		// 2个头像

		if (userData != null) {
			bitmapUtils.display(findViewById(R.id.my_head),
					SystemTools.getHttpFileUrl(userData.getAvatar()));
			bitmapUtils.display(findViewById(R.id.my_head2),
					SystemTools.getHttpFileUrl(userData.getAvatar()));
			// 昵称
			((TextView) findViewById(R.id.my_name)).setText(userData
					.getNickname());
			((TextView) findViewById(R.id.my_name2)).setText(userData
					.getNickname());
		}

	}

	/*
	 * 获取积分信息
	 */
	private void getServiceData() {
		httpUtils.send(HttpMethod.GET, HttpInterfaces.clickPpen + "&mobile="
				+ userData.getMobile() + "&password=" + userData.getPassword()
				+ "&activity_id=" + getIntent().getIntExtra("activity_id", -1),
				new MyRequestCallBack<String>(this,
						getString(R.string.loadding)) {
					@Override
					public void onFailure(HttpException error, String msg) {
						super.onFailure(error, msg);
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						super.onSuccess(responseInfo);
						try {
							JSONObject json = new JSONObject(
									responseInfo.result);
							int status = json.getInt("status");
							if (status == 1) {
								data = JsonHelper.parseObject(json
										.getJSONArray("result")
										.getJSONObject(0),
										HongBaoOpenData.class);
								upDataUi();
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}
				});
	}

	protected void upDataUi() {
		if (data != null) {
			((TextView) findViewById(R.id.jifen)).setText(data.getUser_score()
					.intValue() + "");
			String time[] = data.getAdd_time().split(" ");
			if (time.length > 0) {
				((TextView) findViewById(R.id.time)).setText(time[0]);
			}
			((TextView) findViewById(R.id.mingci)).setText(data.getRownum()
					+ "");

		}

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}
}
